/*
    @Author: feilong
    @Time: 2019-10
    
    @Description: 用户列表
*/

class User {
    constructor(form, layer){
        this.form = form
        this.layer = layer
        this.name = ""
        this.password = ""
    }

    init(){
        
    }
    //添加用户
    addUser(edit){
        var data = {
            title : "添加用户",
            type : 2,
            content : "userAdd.html",
            success : function (layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".user_name").val(edit.user_name);  //登录名
                    body.find(".password").val(edit.password);  //
                    // body.find(".userSex input[value="+edit.userSex+"]").prop("checked","checked");  //性别
                    body.find(".gold").val(edit.gold);  //会员等级
                    body.find(".diamond").val(edit.diamond);    //用户状态
                    
                    body.find(".btn-change-user").show();
                    body.find(".btn-add-user").hide();
                    layui.form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 2
                    });
                },100)
            }
        }
        var index = layui.layer.open(data);
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }    


    //删除用户
    deleteUser(data){
        layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
            $.post("./delete_user",{
                user_id : data.user_id  
            },function(res, status){
                console.log("___res__", res)
                if(res.error_code > 0 ){
                    top.layer.msg(res.error_msg)
                    return
                }
                tableIns.reload();
                layer.close(index);
            })
        });        
    }
}


layui.use(['form','layer','table','laytpl'],function(){
    var form = layui.form;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    $ = layui.jquery;
    var laytpl = layui.laytpl;
    var table = layui.table;

    //用户列表
    var tableIns = table.render({
        elem: '#userList',
        // url : '../../json/userList.json',
        url : "./user_list",
        request: {
            pageName: 'curr' //页码的参数名称，默认：page
            ,limitName: 'nums' //每页数据量的参数名，默认：limit
        },        
        cellMinWidth : 80,
        page : true,
        height : "full-100",
        limits : [20],
        limit : 20,

        id : "userListTable",
        cols : [[
            // {type: "checkbox", fixed:"left", width:50},
            {field: 'user_id', title: 'id', minWidth:100, align:"center"},
            {field: 'user_name', title: '用户名', minWidth:100, align:"center"},
            // {field: 'userEmail', title: '用户邮箱', minWidth:200, align:'center',templet:function(d){
            //     return '<a class="layui-blue" href="mailto:'+d.userEmail+'">'+d.userEmail+'</a>';
            // }},
            {field: 'password', title: '密码', minWidth:100, align:"center"},
            {field: 'gold', title: '金币', minWidth:100, align:"center"},
            {field: 'diamond', title: '钻石', minWidth:100, align:"center"},
            {field: 'online', title: '在线', minWidth:100, align:"center", templet:function(d){
                if(d.online=="1"){
                    return "是";
                }
                return "否";
            }},
            {field: 'nickname', title: '别名', minWidth:100, align:"center"},
            {field: 'register_ip', title: '注册ip', minWidth:100, align:"center"},
            {field: 'register_addr', title: '注册地址', minWidth:100, align:"center"},
            {field: 'register_time', title: '注册时间', minWidth:100, align:"center"},
            {field: 'login_ip', title: '登录ip', minWidth:100, align:"center"},
            {field: 'login_addr', title: '登录地址', minWidth:100, align:"center"},
            {field: 'login_time', title: '登录时间', minWidth:100, align:"center"},

            {field: 'user_type', title: '用户类型', minWidth:100, align:"center"},
            {field: 'status', title: '状态', minWidth:100, align:"center"},
            {field: 'level_id', title: '等级', minWidth:100, align:"center"},
            {field: 'face_id', title: '头像id', minWidth:100, align:"center"},
            {field: 'device_type', title: '设备类型', minWidth:100, align:"center"},
            {field: 'integral', title: '积分', minWidth:100, align:"center"},
            {field: 'position', title: '位置', minWidth:100, align:"center"},
            {field: 'mobile', title: '手机号', minWidth:100, align:"center"},

            {title: '操作', minWidth:120, templet:'#userListBar',fixed:"right",align:"center"}
        ]],
        data : []
    });

    let user = new User(form, layer)
    user.init()

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        if($(".searchVal").val() != ''){
            table.reload("newsListTable",{
                page: {
                    curr: 1 //重新从第 1 页开始
                },
                where: {
                    key: $(".searchVal").val()  //搜索的关键字
                }
            })
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

      
  


    $(".addNews_btn").click(function(){
        user.addUser();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('userListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除文章接口",{
                //     newsId : newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })

    //列表操作
    table.on('tool(userList)', function(obj){
        var layEvent = obj.event;
        var data = obj.data;
        console.log("_______data____", data)
        if(layEvent === 'edit'){ //编辑
            user.addUser(data);
        // }else if(layEvent === 'usable'){ //启用禁用
        //     var _this = $(this),
        //         usableText = "是否确定禁用此用户？",
        //         btnText = "已禁用";
        //     if(_this.text()=="已禁用"){
        //         usableText = "是否确定启用此用户？",
        //         btnText = "已启用";
        //     }
        //     layer.confirm(usableText,{
        //         icon: 3,
        //         title:'系统提示',
        //         cancel : function(index){
        //             layer.close(index);
        //         }
        //     },function(index){
        //         _this.text(btnText);
        //         layer.close(index);
        //     },function(index){
        //         layer.close(index);
        //     });
        }else if(layEvent === 'del'){ //删除
            // user.deleteUser(data)
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                $.post("./delete_user",{
                    user_id : data.user_id  
                },function(res, status){
                    console.log("___res__", res)
                    if(res.error_code > 0 ){
                        top.layer.msg(res.error_msg)
                        return
                    }
                    tableIns.reload();
                    layer.close(index);
                })
            });             
        }
    });


})
